package in.abip.chisame;

import java.util.List;

import in.abip.chisame.model.Hvac;
import in.abip.chisame.model.Light;
import in.abip.chisame.model.Mood;
import in.abip.chisame.model.MoodHvacs;
import in.abip.chisame.model.MoodLights;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Room activity.
 * 
 * @author Julot.
 * @since Thursday, January 26th, 2011.
 */
public class MoodListActivity extends NetworkActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);
        populate();
        bind();
    }
    
    /** Populate room list by item from database. */
    protected void populate() {
        ListView list = (ListView) findViewById(R.id.list);
        ListAdapter adapter = new ListAdapter(
    		this,
    		R.layout.list_item,
    		loadModels()
		);
        list.setAdapter(adapter);
    }
    
	/**
	 * Load lights.
	 * @return {@link List}
	 */
	private List<Object> loadModels() {
		Bundle bundle = getIntent().getExtras();
		List<Object> models = null;
		if (bundle != null) {
			models = Mood.model().findAll(
				"roomId = " + bundle.getInt("roomId")
			);
		}
		else models = Mood.model().findAll();
		return models;
	}
	
    /** Bind room list view to listener. */
	private void bind() {
        OnItemClickListener listener = new OnItemClickListener() {
			public void onItemClick(
				AdapterView<?> parent,
				View itemClicked,
				int position,
				long id
			) {
				ListAdapter adapter = (ListAdapter) parent.getAdapter();
				Mood mood = (Mood) adapter.getItem(position);
				setupLight(mood);
				setupHvac(mood);
				Toast.makeText(
					MoodListActivity.this,
					mood.getListText() + " mood activated.",
					Toast.LENGTH_SHORT
				).show();
			}
		};
        ListView list = (ListView) findViewById(R.id.list);
		list.setOnItemClickListener(listener);
	}
	
	/**
	 * Set up light.
	 * @param mood
	 */
	private void setupLight(Mood mood) {
//		Chachamaru chachamaru = getChachamaru();
		List<Object> models = MoodLights.model().findAll(
			"moodId = " + mood._id
		);
		for (Object model : models) {
			MoodLights moodLights = (MoodLights) model;
			Light light = moodLights.getLight();
			light.setValue(moodLights.value);
//			chachamaru.attention(light.getRunCmd());
		}
	}
	
	private void setupHvac(Mood mood) {
//		Chachamaru chachamaru = getChachamaru();
		List<Object> models = MoodHvacs.model().findAll(
			"moodId = " + mood._id
		);
		for (Object model : models) {
			MoodHvacs moodHvacs = (MoodHvacs) model;
			Hvac hvac = moodHvacs.getHvac();
			hvac.setValue(moodHvacs.value);
//			chachamaru.attention(hvac.getRunCmd());
		}
	}
}
